package com.service;

import com.constant.CategoryConst;
import com.dao.BusinessDao;
import com.dto.BusinessDto;
import com.dto.BusinessListDto;
import com.pojo.Business;
import com.pojo.Page;
import com.util.CommonUtil;
import com.util.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class BusinessService {
    @Resource
    private BusinessDao businessDao;

    @Value("${businessImage.savePath}")
    private String savePath;

    @Value("${businessImage.url}")
    private String url;

    public BusinessDto getById(Long id) {
        BusinessDto result = new BusinessDto();
        Business business = businessDao.selectById(id);
        BeanUtils.copyProperties(business, result);
        result.setImg(url + business.getImgFileName());

        result.setStar(this.getStar(business));

        return result;
    }

    public List<BusinessDto> searchByPage( BusinessDto businessDto, Page page) {
        List<BusinessDto> result = new ArrayList<>();
        Business businessForSelect = new Business();
        BeanUtils.copyProperties(businessDto, businessForSelect);
        page.setTotalNumber(businessDao.count(businessForSelect));
        List<Business> list = businessDao.selectByPage(businessForSelect,(page.getCurrentPage()-1)*page.getPageNumber(),page.getPageNumber());
        for (Business business : list) {
            BusinessDto businessDtoTemp = new BusinessDto();
            businessDtoTemp.setPage(page);
            BeanUtils.copyProperties(business, businessDtoTemp);
            businessDtoTemp.setImg(url + business.getImgFileName());
            businessDtoTemp.setStar(this.getStar(business));
            result.add(businessDtoTemp);
        }
        return result;
    }

    public BusinessListDto searchByPageForApi(BusinessDto businessDto, Page page) {
        BusinessListDto result = new BusinessListDto();

        // 组织查询条件
        Business businessForSelect = new Business();
        BeanUtils.copyProperties(businessDto, businessForSelect);
        // 当关键字不为空时，把关键字的值分别设置到标题、副标题、描述中
        if (!CommonUtil.isEmpty(businessDto.getKeyword())) {
            businessForSelect.setTitle(businessDto.getKeyword());
            businessForSelect.setSubtitle(businessDto.getKeyword());
            businessForSelect.setDesc(businessDto.getKeyword());
        }
        // 当类别为全部(all)时，需要将类别清空，不作为过滤条件
        if (businessDto.getCategory() != null && CategoryConst.ALL.equals(businessDto.getCategory())) {
            businessForSelect.setCategory(null);
        }
        // 前端app页码从0开始计算，这里需要+1
        page.setTotalNumber(businessDao.count(businessForSelect));
        List<Business> list = businessDao.selectLikeByPage(businessForSelect,(page.getCurrentPage()-1)*page.getPageNumber(),page.getPageNumber());

        result.setHasMore(page.getCurrentPage() < page.getTotalPage());

        // 对查询出的结果进行格式化
        for (Business business : list) {
            BusinessDto businessDtoTemp = new BusinessDto();
            businessDtoTemp.setPage(page);
            BeanUtils.copyProperties(business, businessDtoTemp);
            businessDtoTemp.setImg(url + business.getImgFileName());
            // 为兼容前端mumber这个属性
            businessDtoTemp.setMumber(business.getNumber());
            businessDtoTemp.setStar(this.getStar(business));
            result.getData().add(businessDtoTemp);
        }

        return result;
    }

    public boolean add(BusinessDto businessDto) {
        Business business = new Business();
        BeanUtils.copyProperties(businessDto, business);

        if (businessDto.getImgFile() != null && businessDto.getImgFile().getSize() > 0) {
            try {
                String fileName = FileUtil.save(businessDto.getImgFile(), savePath);
                business.setImgFileName(fileName);
                // 默认已售数量为0
                business.setNumber(0);
                // 默认评论总次数为0
                business.setCommentTotalNum(0L);
                // 默认评论星星总数为0
                business.setStarTotalNum(0L);
            } catch (IllegalStateException | IOException e) {
                return false;
            }
            businessDao.insert(business);
            return true;
        } else {
            return false;
        }
    }

    public boolean update(BusinessDto businessDto) {
        Business business = new Business();
        BeanUtils.copyProperties(businessDto, business);
        if (businessDto.getImgFile() != null) {
            try {
                String fileName = FileUtil.save(businessDto.getImgFile(), savePath);
                business.setImgFileName(fileName);
                // 默认已售数量为0
                business.setNumber(0);
                // 默认评论总次数为0
                business.setCommentTotalNum(0L);
                // 默认评论星星总数为0
                business.setStarTotalNum(0L);
            } catch (IllegalStateException | IOException e) {
                return false;
            }
            businessDao.update(business);
            return true;
        } else {
            return false;
        }
    }

    private int getStar(Business business) {
        if(business.getStarTotalNum() != null && business.getCommentTotalNum() != null && business.getCommentTotalNum() != 0) {
            return (int)(business.getStarTotalNum() / business.getCommentTotalNum());
        } else {
            return 0;
        }
    }

    public boolean remove(Long id) {
        Business business = businessDao.selectById(id);
        FileUtil.delete( savePath+ business.getImgFileName());
        return businessDao.delete(id)>0;
    }

}
